<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    var validateStackSequences = function (pushed, popped) {
        if (pushed == null || pushed.length == 0) {
          return true;
        }

        let k = 0;
        let stack = [];
        for (let i = 0; i < pushed.length; i++) {
          stack.push(pushed[i]);
          while (stack.length) {
            let pre = stack.pop();
            if (pre == popped[k]) {
              k++;
            } else {
              stack.push(pre);
            }
          }
        }
        return stack.length == 0;
      };

      validateStackSequences(pushed = [1, 2, 3, 4, 5], popped = [4, 5, 3, 2, 1]);
  </script>
</body>
</html>